package com.tcm.doctor.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.lang.StringUtils;
import com.base.core.utils.R;
import com.tcm.doctor.service.QrcodeService;
import com.tcm.invite.domain.dto.InviteParams;
import com.tcm.invite.service.InviteService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/doctor")
public class QrcodeController {

    @DubboReference(version = "0.0.1-alpha", check = false, lazy = true)
    private QrcodeService qrcodeService;

    @DubboReference(version = "0.0.1-alpha", check = false, lazy = true)
    private InviteService inviteService;


    /**
     * 医生名片
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/card/qrcode", method = RequestMethod.GET)
    public R doctorCardQrcode(@RequestParam("userId") Long userId) throws Exception {
        try {
            Map<String,String> res = qrcodeService.doctorCard(userId);
            return R.success(res);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 医生名片链接(可通过手机号，分享微信等方式加好友)
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/card/url", method = RequestMethod.GET)
    public R doctorCardUrl(@RequestParam("userId") Long userId) throws Exception {
        try {
            String res = qrcodeService.doctorCardUrl(userId);
            return R.success(res);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 医生邀请同行二维码
     *
     * @param cellphone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/invite/qrcode", method = RequestMethod.GET)
    public R inviteQrcode(@RequestParam("cellphone") String cellphone) throws Exception {
        try {
            if(StringUtils.isEmpty(cellphone)){
                return R.fail("缺少登录手机号");
            }
            String qrcode = inviteService.inviteQrcode(cellphone, "doctor");
            return R.success(qrcode);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 医生邀请分享链接
     *
     * @param cellphone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/invite/link", method = RequestMethod.GET)
    public R inviteLink(@RequestParam("cellphone") String cellphone) throws Exception {
        try {
            if(StringUtils.isEmpty(cellphone)){
                return R.fail("缺少登录手机号");
            }
            String url = inviteService.createInviteLink(cellphone, "doctor");
            return R.success(url);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/invite/save", method = RequestMethod.POST)
    public R saveInviteRecord(@RequestBody InviteParams inviteParams){
        try {
            inviteService.saveInviteInfo(inviteParams.getSpreadType(),inviteParams.getParams());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
        return R.success();
    }





}
